﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Threading.Tasks;

namespace CoreWeb
{
    public class AutoImgMiddleware : IMiddleware
    {
        public Task InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context, RequestDelegate next)
        {
            if (context.Request.Path.Value.StartsWith("/autoimg", StringComparison.OrdinalIgnoreCase))
            {
                var parts = context.Request.Path.Value.Split('_');
                Color color = Color.Gray;
                var width = 200;
                var height = 200;
                if (parts.Length == 2)
                {
                    var part = parts[1];
                    var firstLetter = part[0];

                    if (firstLetter == '$' || !char.IsNumber(firstLetter))
                    {
                        color = GetColor(part);
                    }
                    else
                    {
                        width = int.Parse(part);
                        height = width;
                    }
                }
                else if (parts.Length == 3)
                {
                    var part = parts[1];
                    var firstLetter = part[0];
                    if (firstLetter == '$' || !char.IsNumber(firstLetter))
                    {
                        color = GetColor(part);
                        width = int.Parse(parts[2]);
                        height = width;
                    }
                    else
                    {
                        width = int.Parse(part);
                        height = int.Parse(parts[2]);
                    }

                }
                else if (parts.Length == 4)
                {
                    color = GetColor(parts[1]);
                    width = int.Parse(parts[2]);
                    height = int.Parse(parts[3]);
                }

                using (Image image = new Bitmap(width, height))
                {
                    var g = Graphics.FromImage(image);
                    g.Clear(color);
                    context.Response.ContentType = "image/png";
                    image.Save(context.Response.Body, ImageFormat.Png);
                    g.Dispose();
                    return context.Response.Body.FlushAsync();
                }
            }
            else
            {
                return next(context);
            }

        }
        private Color GetColor(string txt)
        {
            if (txt.StartsWith("$"))
            {
                var redStr = txt.Substring(1, 2);
                var greenStr = txt.Substring(3, 2);
                var blueStr = txt.Substring(5, 2);
                var red = int.Parse(redStr, System.Globalization.NumberStyles.HexNumber);
                var green = int.Parse(greenStr, System.Globalization.NumberStyles.HexNumber);
                var blue = int.Parse(blueStr, System.Globalization.NumberStyles.HexNumber);

                return Color.FromArgb(red, green, blue);
            }
            else
            {
                return Color.FromName(txt);
            }
        }
    }
}
